package com.gxa.ssm340.controller;

import com.gxa.ssm340.common.PageDTO;
import com.gxa.ssm340.common.R;
import com.gxa.ssm340.entity.pojo.BrandIntegral;
import com.gxa.ssm340.entity.vo.IntegralVO;
import com.gxa.ssm340.service.IntegralService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author 叶骏
 * @description: 品牌积分控制器
 * @date 2023-10-21 15:35:51
 * @version: 1.0
 */
@Api(value = "IntegralController",tags = "品牌积分接口")
@RestController
@RequestMapping("/integral")
public class IntegralController {
    @Autowired
    private IntegralService integralService;

    @ApiOperation("品牌积分管理列表")
    @GetMapping("/getList")
    public R getAlla( PageDTO pageDTO){
        System.out.println(pageDTO);
        final List<BrandIntegral> byPage = integralService.getByPage(pageDTO);
        System.out.println("===="+byPage);
        final Integer count = integralService.count();
        final IntegralVO integralVO = new IntegralVO();
        integralVO.setCount(count);
        integralVO.setLists(byPage);
        return R.success(integralVO);
    }

    @GetMapping("/exportList")
    public void exportData(@RequestParam("name") String name,
                           @RequestParam("startTime") String startTime,
                           @RequestParam("endTime") String endTime,
                           HttpServletResponse response) throws IOException {
        PageDTO pageDTO = new PageDTO();
        // 设置PageDTO的其他属性
        pageDTO.setName(name);
        pageDTO.setStartTime(startTime);
        pageDTO.setEndTime(endTime);
        // 创建一个工作簿对象
        Workbook workbook = new XSSFWorkbook();

        pageDTO.setPage(1);
        pageDTO.setLimit(1000);
        System.out.println(pageDTO);
        List<BrandIntegral> coupons = integralService.getByPage(pageDTO);
        // 创建一个工作表对象
        Sheet sheet = workbook.createSheet("Data");

        // 创建行和单元格，并将数据写入单元格中
        int rowIndex = 0;
        int cellIndex = 0;
        Row row0 = sheet.createRow(rowIndex++);
        row0.createCell(cellIndex++).setCellValue("序号");
        row0.createCell(cellIndex++).setCellValue("品牌名称");
        row0.createCell(cellIndex++).setCellValue("总积分");
        row0.createCell(cellIndex++).setCellValue("总人数");
        row0.createCell(cellIndex++).setCellValue("总件数");
        for (BrandIntegral item : coupons) {
            Row row = sheet.createRow(rowIndex++);
            cellIndex = 0;
            row.createCell(cellIndex++).setCellValue(item.getId());
            row.createCell(cellIndex++).setCellValue(item.getName());
            row.createCell(cellIndex++).setCellValue(item.getScore());
            row.createCell(cellIndex++).setCellValue(item.getPeople());
            row.createCell(cellIndex++).setCellValue(item.getPcs());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        // 自动调整所有列的单元格大小
        for (int i = 0; i < coupons.get(0).getClass().getDeclaredFields().length; i++) {
            sheet.autoSizeColumn(i);
        }
        // 设置响应头信息，告诉浏览器响应的是一个Excel文件
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=\"品牌积分.xlsx\"");

        // 将Excel文件写入响应输出流中，返回给前端
        workbook.write(response.getOutputStream());
    }

}
